//
//  UIBarButtonItem.swift
//  Weibo-YQY-test
//
//  Created by Lang on 10/10/15.
//  Copyright © 2015 Lang. All rights reserved.
//

import Foundation
import UIKit

extension UIBarButtonItem{
    
    class func item(imageName: String, title: String = "", target:AnyObject?, action:Selector) -> UIBarButtonItem {
        // 初始化一个 button
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        //判断是否有 title
        if title.characters.count > 0{
            // 设置文字以及字体的颜色，以及字体大小
            button.setTitle(title, forState: UIControlState.Normal)
            button.titleLabel?.font = UIFont.systemFontOfSize(14)
            button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
            
        }
        
        // 设置 button 不同状态下的图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        return UIBarButtonItem(customView: button)
    }
    
}